//===============================================================================
// Copyright (C) 2008 Charles River Media. All rights reserved.
// 
// Catmull-Clark subdivision surface viewer program
//
// The developer of the code is
// Gyorgy Antal <gyantal@gmail.com>
//
// Contributor(s):
// Laszlo Szirmay-Kalos <szirmay@iit.bme.hu>
//
// The software was developed for the chapter 
// "Fast Evaluation of Subdivision Surfaces on DirectX10 Graphics Hardware"
// of the book ShaderX 6: Advanced Rendering Techniques, 
// edited by Wolfgang Engel,
// the appropriate section edited by Tom Forsyth. 
//
// The files are provided without support, instruction, or implied warranty of any
// kind.  Charles River Media makes no guarantee of its fitness for a particular purpose and is
// not liable under any circumstances for any damages or loss whatsoever arising
// from the use or inability to use this file or items derived from it.
//===============================================================================
// File: File.h
//
// Description:
//===============================================================================
	
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <string.h>
//#include <iostream>

#define BOOL char
#define FALSE	0
#define TRUE	1

#define BUFFERSIZE      2048

//===================================================================
class File {
//===================================================================
protected:
    char *  name;                  // name of file
    int     fhandle;                // file handle ret by open
    char *  buffer;                 // buffer
    int     ptr;                    // index of actual char in buffer
public:
    File() { buffer = new char[BUFFERSIZE]; ptr = 0; }
    File(char * n); 
    ~File() { 
      	delete buffer;
       	if (fhandle > 2)  
			_close(fhandle); 
    }
    char * GetName() { return name;}
};

class OutputFile;

//===================================================================
class InputFile : public File {
//===================================================================
protected:
    long    file_ptr;               // actual file pointer
	int     nchar;                  // number of chars in buffer
	BOOL    eof;                    // end of file?
	void 	Init() { eof = FALSE; nchar = 0; file_ptr = 0; }
public:
    InputFile() { fhandle = 0; Init(); }   // standard input
	InputFile(char * n);
    ~InputFile() { if (fhandle > 2) _close (fhandle); }
	
	
    void    Initialize(char *);   // Open file of specified name
    char    Read(void);           // Read next char and advance
    char    InspectNext(void);    // Get next char but dont advance
    void    Advance() { Read(); }  // Advance to the next char
    BOOL    IsEOF()  { return eof; }
    void    Reset(); // Re-read file from the beginning
};


